﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Web;

class CustomHostFactory : ServiceHostFactory {
	protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) {
		//throw new Exception("Dimok:" + string.Join("<br>", baseAddresses.Select(a=>a.OriginalString).ToArray())+
		//"<br>Uri:"+HttpContext.Current.Request.Url.OriginalString);
		string errorData = "HttpContext.Current.Request.Url.OriginalString:" + HttpContext.Current.Request.Url.OriginalString + "<br>";
		errorData += string.Join("<br>", baseAddresses.Select(r => r.OriginalString).ToArray())+"<br>";
		try {
			var b = baseAddresses.Single(ba => HttpContext.Current.Request.Url.OriginalString.ToLower().Replace(":80/", "/").StartsWith(ba.OriginalString.ToLower()));
			//Dimok.Exceptions.ASPException.Publish(new Exception(b.OriginalString));
			CustomHost customServiceHost = new CustomHost(serviceType, b);
			return customServiceHost;
		} catch (Exception exc) {
			throw new Exception(errorData, exc);
		}
	}
}

class CustomHost : ServiceHost {
	public CustomHost(Type serviceType, params Uri[] baseAddresses)	: base(serviceType, baseAddresses) { }

	protected override void ApplyConfiguration() {
		base.ApplyConfiguration();
	}
}